package com.yhxt.service.impl;

import com.yhxt.dao.ContractRepository;
import com.yhxt.dto.UnbindDto;
import com.yhxt.eneity.Controct;
import com.yhxt.enums.ContracrEnum;
import com.yhxt.service.ContractService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author zjh
 * @Description //合约 $
 * @Date $ $
 * @Param $
 * @return $
 */
@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractRepository contractRepository;
    /**
     * 三十天合约
     * @param controct
     * @return
     */
    @Override
    public void thirtyContract(Controct controct,int number) {
        BigDecimal bigDecimal = new BigDecimal("0.006");
        BigDecimal newPrincipalNum = controct.getPrincipalNum().multiply(bigDecimal);
        BigDecimal principalNum = vtNumber(newPrincipalNum, number);
        controct.setEarningsNum(principalNum);
        contractRepository.save(controct);
    }

    public BigDecimal vtNumber(BigDecimal newPrincipalNum,int number){
        BigDecimal one = new BigDecimal("0.3");
        BigDecimal two = new BigDecimal("0.1");
        BigDecimal six = new BigDecimal("0.2");
        if(number <= 1 && number >= 0){
            //一代
            return newPrincipalNum.multiply(one);
        }
        if(number <= 3 && number > 1){
            //二代
            return newPrincipalNum.multiply(two);
        }
        if(number >= 6){
            //三代
            return newPrincipalNum.multiply(six);
        }
        return newPrincipalNum;
    }

    /**
     * 60天合约
     * @param controct
     */
    @Override
    public void sixtyContract(Controct controct,int number) {
        BigDecimal bigDecimal = new BigDecimal("0.009");
        BigDecimal newPrincipalNum = controct.getPrincipalNum().multiply(bigDecimal);
        BigDecimal principalNum = vtNumber(newPrincipalNum, number);
        controct.setEarningsNum(principalNum);
        contractRepository.save(controct);
    }

    /**
     * 90天合约
     * @param controct
     */
    @Override
    public void nonetyContract(Controct controct,int number) {
        BigDecimal bigDecimal = new BigDecimal("0.012");
        BigDecimal newPrincipalNum = controct.getPrincipalNum().multiply(bigDecimal);
        BigDecimal principalNum = vtNumber(newPrincipalNum, number);
        controct.setEarningsNum(principalNum);
        contractRepository.save(controct);
    }
    /**
     * 120天合约
     * @param controct
     */
    @Override
    public void twelveContract(Controct controct,int number) {
        BigDecimal bigDecimal = new BigDecimal("0.016");
        BigDecimal newPrincipalNum = controct.getPrincipalNum().multiply(bigDecimal);
        BigDecimal principalNum = vtNumber(newPrincipalNum, number);
        controct.setEarningsNum(principalNum);
        contractRepository.save(controct);
    }

    /**
     * 解约
     * @param dto
     */
    @Override
    public void unbind(UnbindDto dto) {
        Controct controct = (Controct) contractRepository.findOne(dto.getUserId());
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date endTime = null;
        try {
            endTime = simpleFormat.parse(dto.getUnbindTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int poor = poor(controct.getCreateTime(), endTime);
        if(poor != 0){
            //合约类型 1、30日合约 2、60 3、120 4、300
            if (StringUtils.equals(dto.getNumber(), ContracrEnum.BUSINESS_TYPE_1.getCode())) {
                BigDecimal bigDecimal = new BigDecimal("0.10");
                BigDecimal subtract = subtract(bigDecimal, controct.getPrincipalNum());
                controct.setParticularsNum(subtract);
                contractRepository.update(controct);
            }
            if(StringUtils.equals(dto.getNumber(),ContracrEnum.BUSINESS_TYPE_2.getCode())){
                BigDecimal bigDecimal = new BigDecimal("0.15");
                BigDecimal subtract = subtract(bigDecimal, controct.getPrincipalNum());
                controct.setParticularsNum(subtract);
                contractRepository.update(controct);
            }
            if(StringUtils.equals(dto.getNumber(),ContracrEnum.BUSINESS_TYPE_3.getCode())){
                BigDecimal bigDecimal = new BigDecimal("0.20");
                BigDecimal subtract = subtract(bigDecimal, controct.getPrincipalNum());
                controct.setParticularsNum(subtract);
                contractRepository.update(controct);
            }
            if(StringUtils.equals(dto.getNumber(),ContracrEnum.BUSINESS_TYPE_4.getCode())){
                BigDecimal bigDecimal = new BigDecimal("0.35");
                BigDecimal subtract = subtract(bigDecimal, controct.getPrincipalNum());
                controct.setParticularsNum(subtract);
                contractRepository.update(controct);
            }
        }
    }


    /**
     * 计算违约金
     * @param bigDecimal1
     * @param bigDecimal2
     * @return
     */
    public BigDecimal subtract(BigDecimal bigDecimal1,BigDecimal bigDecimal2){
        BigDecimal multiply = bigDecimal2.multiply(bigDecimal1);
        return bigDecimal2.subtract(multiply);
    }
    /**
     * 计算两个时间的天数差
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 两者差的天数
     */
    public int poor(Date startTime,Date endTime){
        long start = startTime.getTime();
        long end = endTime.getTime();
        int days = (int) ((end - start) / (1000 * 60 * 60 * 24));
        return days;
    }

}
